package com.stone.framework.batch.config;

import javax.sql.DataSource;

import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.stone.framework.batch.support.JdbcJobRegistry;

@Configuration
@EnableBatchProcessing
public class BatchConfig extends DefaultBatchConfigurer {

    /**
     * 重写createJobLauncher()，使用异步线程处理job
     */
    @Override
    protected JobLauncher createJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = (SimpleJobLauncher)super.createJobLauncher();
        jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor("stone-batch-job-"));
        return jobLauncher;
    }

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(30);
        taskExecutor.setMaxPoolSize(50);
        taskExecutor.setQueueCapacity(100);
        taskExecutor.setKeepAliveSeconds(600);
        taskExecutor.setThreadNamePrefix("stone-batch-task-");
        return taskExecutor;
    }

    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JdbcJobRegistry jobRegistry) {
        JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor();
        postProcessor.setJobRegistry(jobRegistry);
        return postProcessor;
    }

    @Bean
    public JdbcJobRegistry jdbcJobRegistry(DataSource dataSource) {
        JdbcJobRegistry registry = new JdbcJobRegistry();
        registry.setDataSource(dataSource);
        registry.setServiceName("XXX_BATCH");
        return registry;
    }

    @Bean
    public ConversionService conversionService() {
        FormattingConversionServiceFactoryBean factoryBean = new FormattingConversionServiceFactoryBean(); // 支持@DateTimeFormat
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }

    // @Primary
    // @Bean("afterDefaultJobExecutionListener")
    // public DefaultJobExecutionListener defaultJobExecutionListener() {
    // DefaultJobExecutionListener listener = new DefaultJobExecutionListener();
    // return listener;
    // }
    //
    // @Bean("aroundDefaultJobExecutionListener")
    // public DefaultJobExecutionListener aroundDefaultJobExecutionListener() {
    // DefaultJobExecutionListener listener = new DefaultJobExecutionListener();
    // listener.setFlag(AbstractJobExecutionListener.Flag.ALL);
    // return listener;
    // }

}
